import PIL.Image as Image

def __pilFun(img):
    img = img.convert('RGBA')
    L, H = img.size
    color_0 = img.getpixel((0,0))
    for h in range(H):
        for l in range(L):
            dot = (l,h)
            color_1 = img.getpixel(dot)
            if color_1 == color_0 or color_1 == (204,204,204,255):
                color_1 = color_1[:-1] + (0,)
                img.putpixel(dot,(0,0,0,0))
    return img

# 以第一个像素为准，相同色改为透明
def transparent_back(path,control):
    """将图片的背景修改为透明"""
    img = Image.open(path)
    control.addLog(str(path))
    return __pilFun(img)

def transparent_bSave(path,newName):
    """将图片的背景修改为透明,并根据新的图片名称另存"""
    img = Image.open(path)
    img = __pilFun(img)
    img.save(newName)